const serializer = new XMLSerializer();
export default function svg2png(svg: SVGSVGElement | null) {
  if (svg) {
    const svgData = serializer.serializeToString(svg);
    const image = document.createElement("img");
    const canvas = document.createElement("canvas");
    canvas.width = 400;
    canvas.height = 400;
    const ctx = canvas.getContext("2d");
    image.setAttribute(
      "src",
      "data:image/svg+xml;base64," + btoa(unescape(encodeURIComponent(svgData)))
    );

    image.onload = () => {
      ctx?.drawImage(image, 0, 0);
      const imgSrc = canvas.toDataURL("image/png");

      const a = document.createElement("a");
      a.href = imgSrc;
      a.download = "示例.png";
      a.click();
    };
  }
}
